package uqbar.comm.netty.arduino.protocol.simple;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;

import uqbar.comm.netty.StringUtils;
import uqbar.comm.netty.arduino.SetDigitalPin;
import uqbar.comm.netty.protocol.ChannelBufferFactory;
import uqbar.comm.netty.protocol.OutputConverter;

@OutputConverter(from = { SetDigitalPin.class }, to = { "SD{pin(nn)}{value(n)}" })
public class SimpleSetDigitalPinFactory implements ChannelBufferFactory {

	public ChannelBuffer transform(final Object output) {
		SetDigitalPin command = (SetDigitalPin) output;
		ChannelBuffer buffer = ChannelBuffers.buffer(5);
		buffer.writeBytes(("SD" + this.pinToString(command) + command.getValue().asInt()).getBytes());
		return buffer;

	}

	protected String pinToString(final SetDigitalPin command) {
		return StringUtils.fillNumber(2, new Integer(command.getPin()).toString());
	}

	public boolean canTransform(final Object output) {
		return output instanceof SetDigitalPin;
	}

}
